<?php
namespace ShopApi\Controller;
use Common\Controller\HomebaseController;
use Guzzle\Http\Message\Header;
use Think\Image;
class ShopController extends HomebaseController {
     
    protected $model;
    function __construct(){
          $this->model = D('shop');  
    }
    
    function query() {
        $where = "";
        if($_GET['floorid']!=0){
            $where = "where floorid = ".$_GET['floorid'];
        }
        $sql = "select a.*,b.name floorname,c.name formatname,d.savename,d.savepath  from ds_shop a left join ds_floor b on a.floorid = b.id 
            left join ds_format c on a.formatid = c.id left join ds_attachment d on a.attachmentID = d.id ".$where." order by a.floorid asc";
        $shops =  $this->model->query($sql);
        echo json_encode($shops);
     }
     
     function query_one(){
         $sql  = "select a.*,b.savename,b.savepath from ds_shop a left join ds_attachment b on a.attachmentID = b.id where a.id = ".$_GET['shopid']." limit 1";
         $shop = $this->model->query($sql);
         echo json_encode($shop[0]);
     }
     
     function query_floor(){
         $floors = D('floor')->order("`order` asc")->select();
         echo json_encode($floors);
     }
     
     function query_format(){
         $formats = D('format')->order("`order` asc")->select();
         echo json_encode($formats);
     }
     
     function addshop(){
         header("Access-Control-Allow-Origin: *");
         $data = $_POST;
         $data['formatid'] = implode(",",$data['formatid']);
         $data['content'] = htmlspecialchars(str_replace(array("/r/n", "/r", "/n","'"),"",$data['content']));
         $sql = "insert into ds_shop (shopName,floorID,formatID,address,number,sp,qp,jp,content,writer,attachmentID,create_time) values 
             ('".$data['shopname']."','".$data['floorid']."','".$data['formatid']."','".$data['address']."','".$data['number']."',
                 '".$data['sp']."','".$data['qp']."','".$data['jp']."','".$data['content']."','".$data['writer']."','".$data['attachmentID']."','".date('Y-m-d H:i:s')."') ";
        $res = $this->model->execute($sql);
        $myfile = fopen("Log.txt","a+");
        fwrite($myfile,"[".time()."]".$sql."__".$res."\r\n");
        fclose($myfile);
        $str = "";
        if($res){
             $str = "店铺添加成功！";
         }else{
             $str = "店铺添加失败，检查铺位号是否存在！";
         }
         echo $str; 
     }
     
     function editshop(){
          header("Access-Control-Allow-Origin: *");
         $data = $_POST;
         $data['formatid'] = implode(",",$data['formatid']);
         $attachment = "";
         if($data['attachmentID']){
           $attachment =  ",attachmentID='".$data['attachmentID']."'";
         }
         $data['content'] = str_replace(array("/r/n", "/r", "/n","'"),"",$data['content']);
         $sql = "update ds_shop set shopName='".$data['shopname']."',floorID='".$data['floorid']."',formatID='".$data['formatid']."',address='".$data['address']."',
             number='".$data['number']."',sp='".$data['sp']."',qp='".$data['qp']."',jp='".$data['jp']."',content='".$data['content']."',
                 writer='".$data['writer']."'".$attachment.",`update_time`='".date("Y-m-d H:i:s")."' where id=".$data['id'];
         $res = $this->model->execute($sql);
         $myfile = fopen("Log.txt","a+");
         fwrite($myfile,"[".time()."]".$sql."__".$res."\r\n".$data['content']."\r\n");
         fclose($myfile);
         $str = "";
         if($res){
             $str = "店铺编辑成功！";
         }else{
             $str = "店铺编辑失败！";
         }
         echo $str;
     }
     
     function imgupload(){
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Headers: Cache-Control,Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Expires, Content-Type, X-E4M-With");
        
        $baseurl = "http://ds.dtshy.win:8080";
        
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728000000; // 设置附件上传大小
        $upload->exts = array(
            'jpg',
            'gif',
            'png',
            'jpeg'
        ); // 设置附件上传类型
        $upload->rootPath = "./uploads/";
        $upload->savePath = "/shop/"; // 设置附件上传目录
        
        $info = $upload->upload();
        $name = "file";
        
        $attachment = M('attachment')->add($info[$name]);
        
        echo $attachment;
     }
     
     function ajaxthumb(){
        header("Access-Control-Allow-Origin: *");
        $attachment = M('attachment')->find($_GET['attacid']);
         $path = "./uploads".$attachment['savepath'];
         $file = "./uploads".$attachment['savepath'].$attachment['savename'];
         $image = new Image();
         $image->open($file);
         $image->thumb(700, 700)->save($path.l_.$attachment['savename']);
         $image->thumb(400, 400)->save($path.m_.$attachment['savename']);
         $image->thumb(150, 150)->save($path.s_.$attachment['savename']);
     }
     
     function del(){
         header("Access-Control-Allow-Origin: *");
         $id = I('get.id');
         $res = $this->model->delete($id);
         if($res){
             echo 1;
         }else{
             echo 0;
         }
     }
     
     function turnImg(){
         //echo I('post.source')."旋转".I('post.flag');
         $source = I('post.source');
         $flag = I('post.flag');
         $res = $this->rotate($source,$flag);
         if($res){
            $this->thumb($source);
            echo "旋转成功，浏览器缓存可能导致图片不能及时更改！";
         }else{
             echo "旋转失败！";
         }
         
     }
     
     function thumb($file){
         $image = new Image();
         $image->open($file);
         $image->thumb(700, 700)->save(dirname($file)."/".l_.basename($file));
         $image->thumb(400, 400)->save(dirname($file)."/".m_.basename($file));
         $image->thumb(150, 150)->save(dirname($file)."/".s_.basename($file));
     }
     
     function rotate($filename,$degrees){
         //创建图像资源，以jpeg格式为例
         $source = imagecreatefromjpeg($filename);
         //使用imagerotate()函数按指定的角度旋转
         $rotate = imagerotate($source, $degrees, 0);
         //旋转后的图片保存
         return imagejpeg($rotate,$filename);
     }
     
 }